package com.zyz.I_多线程.线程池;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author: vick
 * @Date: 2025/3/24
 * @Description:
 * @version: 1.0
 */
public class NumberThreadTest {
    public static void main(String[] args) {
        //1. 提供指定线程数量的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        //设置线程池的属性,例如 线程池中的线程数上限
        //ThreadPoolExecutor executorService1 = (ThreadPoolExecutor) executorService;
        //executorService1.setMaximumPoolSize(50);

        // 2. 执行指定的线程操作
        //Runnable
        NumberThread numberThread = new NumberThread();
        executorService.execute(numberThread);


        try {
            //Callable
            NumberThread2 numberThread2 = new NumberThread2();
            Future future = executorService.submit(numberThread2);
            System.out.println("总和为：" + future.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }

        //3. 关闭线程池连接
        executorService.shutdown();
    }
}
